<template>
  <div>
    <van-list
      v-model="tableLoading"
      :finished="finished"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ul>
        <li v-for="(item, index) in tableData" :key="index" class="model_content_first model_content gl_clear">
          <div class="left gl_fl">
            <span class="info_rank">{{ item.finalRank }}</span>
            <img class="ingo_logo" :src="getStaticFileUrl(item.photoUrl)" @error="errorPhoto">
          </div>
          <div class="right">
            <div class="info_name gl_ellipsis">{{ item.name }}</div>
            <div class="info_gold">
              <div>
                <img class="img_gold" src="@/assets/images/league/mb_gold.png">
                <span class="txt_gold">{{ item.subRankList[0] ? item.subRankList[0].subRankCount : '' }}</span>
              </div>
              <div>
                <img class="img_gold" src="@/assets/images/league/mb_silver.png">
                <span class="txt_gold">{{ item.subRankList[1] ? item.subRankList[1].subRankCount : '' }}</span>
              </div>
              <div>
                <img class="img_gold" src="@/assets/images/league/mb_bronze.png">
                <span class="txt_gold">{{ item.subRankList[2] ? item.subRankList[2].subRankCount : '' }}</span>
              </div>
            </div>
            <div>
              <span class="info_score">联赛积分 <span style="font-weight: 500">{{ item.finalPoints }}</span></span>
            </div>
          </div>
        </li>
      </ul>
    </van-list>
  </div>
</template>

<script>
import {
  api_GET_EventsManagementEventResultUnifiedPointRankList
} from '@/extend/api/gameswaggerApi/app';

import {
  Api_GET_EventsManagementEventResultUnifiedPointRankListQO
} from '@/lib/form/game/swaggerForm';

import defaultPhoto from '@/assets/images/face_default.png';

export default {
  props: {
    gameInfo: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    return {
      defaultPhoto: defaultPhoto, // 默认照片
      pageLoading: false,
      tableLoading: false,
      finished: false,
      error: false,
      tableData: [],
      total: 0,
      qo: new Api_GET_EventsManagementEventResultUnifiedPointRankListQO()
    };
  },
  watch: {
    gameInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.id) {
          this.getPageList();
        }
      }
    }
  },
  methods: {
    onLoad() {
      let _page = this.qo.Page || 1;
      _page++;
      this.qo.Page = _page;
      this.getPageList(_page);
    },
    // 获取页面数据
    getPageList (page) {
      const _qo = JSON.parse(JSON.stringify(this.qo));
      _qo.Page = page || 1;
      _qo.SkipCount = (_qo.Page - 1) * _qo.MaxResultCount;
      _qo.CompetitionId = this.gameInfo.id;
      _qo.Type = 'Delegation';
      _qo.EventTypeCode = 'NoneCompetitive';
      _qo.IncludeFinal = true;
      _qo.IncludeSubRank = true;
      console.log('_qo: ', _qo);
      this.apiRankList(_qo);
    },
    // UnifiedPointRankList
    apiRankList(qo) {
      this.tableLoading = true;
      api_GET_EventsManagementEventResultUnifiedPointRankList(qo)
        .then(
          res => {
            this.tableLoading = false;
            this.total = res.totalCount;
            this.tableData.push(...res.items);
            console.log(res);

            // 数据全部加载完成
            if (this.tableData.length >= this.total) {
              this.finished = true;
            }
          }
        )
        .catch(
          error => {
            this.error = true;
            this.tableLoading = false;
            console.log(error);
          }
        );
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPhoto;
      img.onerror = null;
    }
  }
};
</script>

<style lang="scss" scoped>
/* 模块样式 */
  .main_content .model_content {
    margin-bottom: 5px;
  }
  .model_content_first {
    padding: 18px 7px 12px;
  }

/* 排名与Logo; 独立模块 */
  .info_rank {
    display: inline-block;
    width: 18px;
    height: 18px;
    line-height: 18px;
    font-size: 14px;
    color: #E02020;
    background-color: #ffe7e7;
    border-radius: 50%;
    text-align: center;
    margin-right: 9px;
  }
  .ingo_logo {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }
  .info_name {
    width: 100%;
    font-size: 15px;
    font-weight: 400;
    text-align: left;
    line-height: 21px;
    margin-bottom: 6px;
  }
  .info_gold {
    display: flex;
    padding-right: 30px;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  .info_score {
    display: inline-block;
    padding: 1px 6px;
    font-size: 13px;
    font-weight: 400;
    text-align: left;
    color: #c28e00;
    line-height: 19px;
    background-color: #fff3d1;
    border-radius: 5px;
  }
  .img_gold {
    width: 13px;
    height: 19px;
    vertical-align: top;
    margin-right: 8px;
  }
  .txt_gold {
    display: inline-block;
    min-width: 26px;
    font-size: 14px;
    font-weight: 400;
    text-align: left;
    color: #666666;
    line-height: 20px;
    vertical-align: top;
  }

/* 模块差异 */
  .model_content_first .right {
    margin-left: 109px;
  }
  .model_content_first .left {
    display: flex;
    padding-top: 11px;
    align-items: center;
  }

</style>
